﻿//////////////////////////////////////////////////////////////////////////////
//                                                                          //
// Copyright 2010 ZGDev Software                                            //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//   http://www.apache.org/licenses/LICENSE-2.0                             //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZGDev.FormSpark.AnimationCurves
{
    /// <summary>
    /// Represents a linear timing function for animations.
    /// </summary>
    /// <seealso cref="IAnimationCurve"/>
    /// <seealso cref="AnimationCurve"/>
    public class LinearCurve : IAnimationCurve
    {
        private float m_duration = 1.0f;
        private float m_elapsed;

        #region IAnimationCurve Members

        /// <summary>
        /// Sets the duration of the animation curve.
        /// </summary>
        /// <param name="secs">The duration in seconds.</param>
        public void SetDuration(float secs)
        {
            this.m_duration = secs;
            this.m_elapsed = 0.0f;
        }

        /// <summary>
        /// Gets the duration of the animation curve.
        /// </summary>
        /// <returns>The duration of the animation curve (in seconds).</returns>
        public float GetDuration()
        {
            return this.m_duration;
        }

        /// <summary>
        /// Gets the current elapsed time for the animation curve. This is updated
        /// with every call to |Update|.
        /// </summary>
        /// <returns>The current elapsed time (in seconds).</returns>
        /// <see cref="Update"/>
        public float GetElapsed()
        {
            return this.m_elapsed;
        }

        /// <summary>
        /// Sets the current elapsed time.
        /// </summary>
        /// <param name="secs">The elapsed time in seconds.</param>
        public void SetElapsed(float secs)
        {
            this.m_elapsed = secs;
        }

        /// <summary>
        /// Resets the elapsed time.
        /// </summary>
        public void Reset()
        {
            this.m_elapsed = 0.0f;
        }

        /// <summary>
        /// Updates the elapsed time and returns the current interpolation
        /// value (where inbetween our to [1.0] and from [0.0] values we are at).
        /// </summary>
        /// <param name="timeDelta">
        /// The number of seconds that have elapsed since our last call to |Update|,
        /// or the amount of time we want to increase our elapsed time by.
        /// </param>
        /// <returns>The current interpolation value.</returns>
        public float Update(float timeDelta)
        {
            this.m_elapsed += timeDelta;

            if (this.m_elapsed > this.m_duration)
            {
                this.m_elapsed = this.m_duration;
            }

            // the linear curve is simple to calculate,
            // our interpolation value is simply the %complete
            // our animation is (elapsed / duration)

            return this.m_elapsed / this.m_duration;
        }

        #endregion
    }
}
